﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIMain : MonoBehaviour 
{
    private Button btn_Start;
    private Button btn_Shop;
    private Image skinButtonImage;
    private Button btn_ResetUserData;
    private Button btn_Rank;
    private Button btn_Silent;
    private bool isSilent;// 是否静音
    public Sprite[] sprite_Silent;// 静音按钮图片
    private Button btn_Quit;

    private void Awake()
    {
        Init();
    }

    
    /// <summary>
    /// 初始化UIMain
    /// </summary>
    private void Init()
    {
        EventCenter.AddListener(CL.EventType.ShowUIMain, ShowUIMain);
        EventCenter.AddListener(CL.EventType.HideUIMain, HideUIMain);
        EventCenter.AddListener<int>(CL.EventType.UpdateSkinButtonImage, UpdateSkinButtonImage);


        btn_Quit = transform.Find("Top/Btn_Quit").GetComponent<Button>();
        btn_Quit.onClick.AddListener(Btn_Quit);
        btn_Start = transform.Find("Bottom/Btn_Start").GetComponent<Button>();
        btn_Start.onClick.AddListener(Btn_Start);
        btn_Shop = transform.Find("Bottom/Btn_Shop").GetComponent<Button>();
        btn_Shop.onClick.AddListener(Btn_Shop);
        skinButtonImage = transform.Find("Bottom/Btn_Shop/Image").GetComponent<Image>();
        btn_ResetUserData = transform.Find("Bottom/Btn_ResetUserData").GetComponent<Button>();
        btn_ResetUserData.onClick.AddListener(Btn_ResetUserData);
        btn_Rank = transform.Find("Bottom/Btn_Rank").GetComponent<Button>();
        btn_Rank.onClick.AddListener(Btn_Rank);
        btn_Silent = transform.Find("Bottom/Btn_Silent").GetComponent<Button>();
        btn_Silent.onClick.AddListener(Btn_Silent);

       
    }
    private void Start()
    {
        //初始化一下界面（写在Awake里面会出现AddListener运行在UIMainOpen后面的情况，导致一些界面未在一开始就隐藏）
        GameManager.Instance.UIMainOpen();
    }


    private void OnDestroy()
    {
        EventCenter.RemoveListener(CL.EventType.ShowUIMain, ShowUIMain);
        EventCenter.RemoveListener(CL.EventType.HideUIMain, HideUIMain);
        EventCenter.RemoveListener<int>(CL.EventType.UpdateSkinButtonImage, UpdateSkinButtonImage);
    }

    /// <summary>
    /// 显示、隐藏主界面UIMain
    /// </summary>
    private void ShowUIMain()
    {
        gameObject.SetActive(true);          
    }
    private void HideUIMain()
    {
        gameObject.SetActive(false);       
    }


    /// <summary>
    /// 退出游戏按钮点击
    /// </summary>
    private void Btn_Quit()
    {
        Application.Quit();
    }

    /// <summary>
    /// 开始按钮点击
    /// </summary>
    private void Btn_Start()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIGameOpen();
        GameManager.Instance.OnGameStart();    
    }

    /// <summary>
    /// 商店按钮点击
    /// </summary>
    private void Btn_Shop()
    {
        GameManager.Instance.UIShopOpen();
    }

    /// <summary>
    /// 重置（归零）玩家数据按钮点击
    /// </summary>
    private void Btn_ResetUserData()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIResetUserDataOpen();
    }

    /// <summary>
    /// 排行榜按钮点击
    /// </summary>
    private void Btn_Rank()
    {
        AudioManager.Instance.PlaySound("button");
        GameManager.Instance.UIRankOpen();
    }

    /// <summary>
    /// 声音按钮点击
    /// </summary>
    private void Btn_Silent()
    {
        AudioManager.Instance.PlaySound("button");
        if (isSilent)
        {
            btn_Silent.transform.GetChild(0).GetComponent<Image>().sprite = sprite_Silent[0];
            AudioManager.Instance.SetGameVolume(1);
        }
        else
        {
            btn_Silent.transform.GetChild(0).GetComponent<Image>().sprite = sprite_Silent[1];
            AudioManager.Instance.SetGameVolume(0);
        }
        isSilent = !isSilent;
        DataManager.Instance.IsSilent(isSilent);
    }

    /// <summary>
    /// 更新皮肤按钮的图片显示
    /// </summary>
    private void UpdateSkinButtonImage(int indexSkin)
    {
        skinButtonImage.sprite = ResManager.Instance.playerSkin[indexSkin];
    }

    
}
